package vip.xiaonuo.modular.manage.tsdatarecord.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import vip.xiaonuo.modular.manage.tsdatarecord.entity.BaseDataTable;
import vip.xiaonuo.modular.manage.tsdatarecord.mapper.BaseDataTableMapper;
import vip.xiaonuo.modular.manage.tsdatarecord.parser.TableColumn;
import vip.xiaonuo.modular.manage.tsdatarecord.service.BaseDataTableService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * BaseDataTableServiceImpl
 *
 * @author JackLee
 * 2022/8/8 16:27
 */
@Service
@Slf4j
public class BaseDataTableServiceImpl extends ServiceImpl<BaseDataTableMapper, BaseDataTable> implements BaseDataTableService {

    @Override
    public void saveByTableColumns(List<TableColumn> columnList, Long recordId) {
        long count = this.getBaseMapper().selectCountByRecordId(recordId);
        if (count == 0 && columnList != null && columnList.size() > 0) {
            List<BaseDataTable> baseDataTables = columnList.stream().map(c -> c.transform(recordId)).collect(Collectors.toList());
            this.saveBatch(baseDataTables);
        }
    }

    @Override
    public List<BaseDataTable> findListByRecordId(Long recordId) {
        QueryWrapper<BaseDataTable> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseDataTable::getRecordId, recordId).orderByAsc(BaseDataTable::getSort);
        return this.list(queryWrapper);
    }

    @Override
    public void updateList(List<BaseDataTable> dataTables) {
        if (CollectionUtil.isNotEmpty(dataTables)) {
            this.updateBatchById(dataTables);
        }
    }
}
